package com.wajahatkarim3.rdbexplorer.demo.slice;

import com.wajahatkarim3.rdbexplorer.slice.RdbExplorerAbilitySlice;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class RdbExplorerAbilitySliceTest {

    @Test
    public void setRdbStore() {
        String dbName = "TodoStore.db";
        StoreConfig config = StoreConfig.newDefaultConfig(dbName);
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DatabaseHelper helper = new DatabaseHelper(context);
        RdbStore rdbStore = helper.getRdbStore(config, 1, null);
        RdbExplorerAbilitySlice.setRdbStore(rdbStore);
        Assert.assertNotNull(rdbStore);
        Assert.assertEquals(rdbStore, RdbExplorerAbilitySlice.rdbStore);
    }

    @Test
    public void getData() {
        String dbName = "TodoStore.db";
        String insertSql = "insert into todo_table (id, title) values (1, 'name')";
        String querySql = "select * from todo_table";
        StoreConfig config = StoreConfig.newDefaultConfig(dbName);
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        RdbExplorerAbilitySlice explorer = new RdbExplorerAbilitySlice();
        DatabaseHelper helper = new DatabaseHelper(context);
        RdbStore rdbStore = helper.getRdbStore(config, 1, callback);
        explorer.setRdbStore(rdbStore);
        try {
            synchronized (this) {
                explorer.getData(insertSql);
                Thread.sleep(2000);
                ArrayList<ResultSet> list = explorer.getData(querySql);
                ResultSet rs = list.get(0);
                rs.goToFirstRow();
                Assert.assertEquals(rs.getInt(0), 1);
                Assert.assertEquals(rs.getString(1), "name");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void isDoubleClickTest() {
        try {
            synchronized (this) {
                Class<?> clazz = Class.forName("com.wajahatkarim3.rdbexplorer.slice.RdbExplorerAbilitySlice");
                Method method = clazz.getDeclaredMethod("isDoubleClick", null);
                method.setAccessible(true);
                RdbExplorerAbilitySlice rdbExplorerAbilitySlice = (RdbExplorerAbilitySlice) clazz.newInstance();
                Object obj1 = method.invoke(rdbExplorerAbilitySlice, null);
                Assert.assertEquals(obj1.toString(), "false");
                Thread.sleep(500);
                Object obj2 = method.invoke(rdbExplorerAbilitySlice, null);
                Assert.assertEquals(obj2.toString(), "true");
            }
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static final RdbOpenCallback callback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore store) {
            store.executeSql("CREATE TABLE IF NOT EXISTS todo_table (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL)");
        }
        @Override
        public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
        }
    };
}